<template>
    <div title="竞赛管理">
        <el-form inline="true">
          <el-form-item label="创建日期" prop="datetimerange">
            <el-date-picker style="width: 240px" v-model="datetimeRange" type="datetimerange" range-separator="至"
              start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
          <el-form-item label="竞赛名称">
            <el-input v-model="params.title" placeholder="请您输入要搜索的竞赛名称" />
          </el-form-item>
          <el-form-item>
            <el-button @click="onSearch" plain >搜索</el-button>
            <el-button @click="onReset" plain type="info">重置</el-button>
            <el-button type="primary" :icon="Plus" plain @click="onAddExam">添加竞赛</el-button>
          </el-form-item>
        </el-form>
        <!-- 表格 -->
        <el-table height="526px" :data="examList" >
          <el-table-column prop="title" label="竞赛标题"/>
          <el-table-column prop="startTime" width="180px" label="竞赛开始时间" />
          <el-table-column prop="endTime" width="180px" label="竞赛结束时间" />
          <el-table-column label="是否开赛" width="100px">
            <template #default="{ row }">
              <div v-if="!isNotStartExam(row)">
                <el-tag type="warning">已开赛</el-tag>
              </div>
              <div v-else>
                <el-tag type="info">未开赛</el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="status" width="100px" label="是否发布">
  
            <template #default="{ row }">
              <div v-if="row.status == 0">
                <el-tag type="danger">未发布</el-tag>
              </div>
              <div v-if="row.status == 1">
                <el-tag type="success">已发布</el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="createName" width="140px" label="创建用户" />
          <el-table-column prop="createTime" width="180px" label="创建时间" />
          <el-table-column label="操作" width="180px"  fixed="right">
  
            <template #default="{ row }">
              <el-button v-if="isNotStartExam(row)" type="text" @click="onEdit(row.examId)">编辑
                </el-button>
              <el-button v-if="isNotStartExam(row)" type="text" @click="onDelete(row.examId)">删除
              </el-button> 
              <el-button v-if="isNotStartExam(row) && row.status == 1" type="text"
                @click="cancelPublishExamInList(row.examId)">撤销发布</el-button>
              <el-button 
                type="text" v-if="isNotStartExam(row) && row.status == 0" 
                @click="publishExamInList(row.examId)">发布</el-button>
              <el-button v-if="!isNotStartExam(row)" type="text"  style="color: #999;">已开赛，不允许操作</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination small v-model:current-page="params.pageNum" v-model:page-size="params.pageSize"
          :page-sizes="[1,2, 5, 10, 15, 20]" :background="true" layout="total, sizes, prev, pager, next, jumper"
          :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange"
          style="margin-top: 20px; justify-content: flex-end" />
    </div>
  </template>
  
  <script setup>
  import { reactive, ref } from "vue"
  import { getExamListService, delExamService, publishService, unPublishService} from "@/apis/exam";
  import router from "@/router";
  import { ElMessage } from "element-plus";
  
    const params = reactive({
        pageNum: 1,
        pageSize: 10,
        startTime: '',
        endTime: '',
        title: '',
    })

    const examList = ref([])
    const total = ref(0);
    const datetimeRange = ref([])

    async function getExamList(){
        if(datetimeRange.value[0] instanceof Date){
            params.startTime = datetimeRange.value[0].toISOString()
        }
        if(datetimeRange.value[1] instanceof Date){
            params.endTime = datetimeRange.value[1].toISOString()       
        }
        const result = await getExamListService(params)
        examList.value = result.rows
        total.value = result.total;
    }

    getExamList();

    function handleSizeChange(newSize){
        params.pageNum = 1;
        getExamList();
    }

    function handleCurrentChange(newPage){
        getExamList();
    }

    function onSearch(){
      getExamList();
      params.pageNum = 1;
    }

    function onReset(){
      params.pageNum = 1,
      params.pageSize = 10,
      params.title = '',
      params.startTime = '',
      params.endTime = '',
      datetimeRange.value.length = 0
      getExamList();
    }

    function isNotStartExam(row){
      const now = new Date(); //当前时间
      return new Date(row.startTime) > now;
    }

    function onAddExam(){
      router.push("/oj/layout/updateExam?type=add")
    }

    async function onEdit(examId){
      router.push(`/oj/layout/updateExam?type=edit&examId=${examId}`)
    }

    async function onDelete(examId) {
      await delExamService(examId)
      ElMessage.success("删除成功");
      params.pageNum = 1
      getExamList()
    }

    async function publishExamInList(examId){
      await publishService(examId);
      ElMessage.success("发布成功");
      getExamList();
    }

    async function cancelPublishExamInList(examId){
      await unPublishService(examId);
      ElMessage.success("撤销发布成功");
      getExamList();
    }
  </script>